package com.gateway.app.common;


import com.alibaba.fastjson2.JSON;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回消息体.
 *
 * @author guoguifang
 * @version 1.6.1
 * @date 2020/8/17 20:13
 */
@SuppressWarnings("ALL")
public class RspEntity<T> {

    private final Header header = new Header();

    private T body;

    public static <T> RspEntity<T> ofSuccess() {
        return new RspEntity<>(CodeConstants.DEFAULT_SUCCESS_CODE, CodeConstants.DEFAULT_SUCCESS_MSG, null);
    }

    public static <T> RspEntity<T> ofSuccess(String retMsg) {
        return new RspEntity<>(CodeConstants.DEFAULT_SUCCESS_CODE, retMsg, null);
    }

    public static <T> RspEntity<T> ofSuccess(T body) {
        RspEntity<T> easyMsResponseEntity = ofSuccess();
        easyMsResponseEntity.setBody(body);
        return easyMsResponseEntity;
    }

    public static <T> RspEntity<T> ofSuccess(String retMsg, T body) {
        RspEntity<T> easyMsResponseEntity = ofSuccess(retMsg);
        easyMsResponseEntity.setBody(body);
        return easyMsResponseEntity;
    }

    public static <T> RspEntity<T> ofFail() {
        return new RspEntity<>(CodeConstants.DEFAULT_ERROR_CODE, CodeConstants.DEFAULT_ERROR_MSG, null);
    }

    public static <T> RspEntity<T> ofFail(String retCode) {
        return new RspEntity<>(retCode, CodeConstants.DEFAULT_ERROR_MSG, null);
    }

    public static <T> RspEntity<T> ofFail(String retCode, String retMsg) {
        return new RspEntity<>(retCode, retMsg, null);
    }

    public static <T> RspEntity<T> ofFail(String retCode, String retMsg, String errorDesc) {
        return new RspEntity<>(retCode, retMsg, errorDesc);
    }

    public boolean isSuccess() {
        return CodeConstants.DEFAULT_SUCCESS_CODE.equals(getRetCode());
    }

    public T getBody() {
        return this.body;
    }

    public void setBody(T body) {
        this.body = body;
    }

    public String getRetCode() {
        return this.header.retCode;
    }

    public void setRetCode(String retCode) {
        this.header.retCode = retCode;
    }

    public String getRetMsg() {
        return this.header.retMsg;
    }

    public void setRetMsg(String retMsg) {
        this.header.retMsg = retMsg;
    }

    public String getErrorDesc() {
        return this.header.errorDesc;
    }

    public void setErrorDesc(String errorDesc) {
        this.header.errorDesc = errorDesc;
    }

    public String getTraceId() {
        return this.header.traceId;
    }

    public void setTraceId(String traceId) {
        this.header.traceId = traceId;
    }

    public Map<String, Object> toMap() {
        Map<String, Object> objectMap = new HashMap<>(8);
        objectMap.put("success", isSuccess());
        objectMap.put("retCode", getRetCode());
        objectMap.put("retMsg", getRetMsg());
        objectMap.put("errorDesc", getErrorDesc());
        objectMap.put("traceId", getTraceId());
        objectMap.put("body", getBody());
        return objectMap;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("{\"success\":").append(isSuccess()).append(",\"retCode\":\"").append(getRetCode()).append("\"");

        String retMsg = getRetMsg();
        if (!StringUtils.isEmpty(retMsg)) {
            stringBuilder.append(",\"retMsg\":\"").append(retMsg).append("\"");
        }

        String errorDesc = getErrorDesc();
        if (!StringUtils.isEmpty(errorDesc)) {
            stringBuilder.append(",\"errorDesc\":\"").append(errorDesc).append("\"");
        }

        String traceId = getTraceId();
        if (!StringUtils.isEmpty(traceId)) {
            stringBuilder.append(",\"traceId\":\"").append(traceId).append("\"");
        }

        if (body != null) {
            stringBuilder.append(",\"body\":");
            /*if (body.getClass().isAnnotationPresent(CustomToString4Log.class)) {
                stringBuilder.append(body.toString());
            } else {
                stringBuilder.append(JSON.toJSONString(body));
            }*/
            stringBuilder.append(JSON.toJSONString(body));
        }

        return stringBuilder.append("}").toString();
    }

    private RspEntity() {
    }

    private RspEntity(String retCode, String retMsg, String errorDesc) {
        this.header.retCode = retCode;
        this.header.retMsg = retMsg;
        this.header.errorDesc = errorDesc;
    }

    private static class Header {

        String retCode;

        String retMsg;

        String errorDesc;

        String traceId;
    }
}
